﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Threading;
using System.Data;
using System.Windows.Threading;
using System.IO;
using System.Net;
using EnterpriseDT.Net.Ftp;
//using System.Net.FtpClient;

namespace VideoArchive
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public BackgroundWorker worker = new BackgroundWorker();
        Config_frm cf;
        bool move = false;
        private System.Drawing.Point pt = new System.Drawing.Point();
        public string mediaserver="";
        public string user="";
        public string password = "";
        public string path = "";
        public string folder = "";
        DataBase dbe;

        public MainWindow()
        {
            InitializeComponent();
            progressBar1.Visibility = Visibility.Hidden;
            progressLabel.Visibility = Visibility.Hidden;
            worker.DoWork += worker_DoWork;
            worker.RunWorkerCompleted += worker_RunWorkerCompleted;
            worker.WorkerReportsProgress = true;
            worker.WorkerSupportsCancellation = true;
            worker.ProgressChanged += backgroundWorker1_ProgressChanged;
            string server = "192.168.100.100\\SQLEXPRESS";
            string dbuser = "sa";
            string pass = "tornado";
            string database = "PASystem";
            dbe = new DataBase(server, dbuser, pass, database);
          //  DataTable tb = dbe.getTable("select [Lietuviškas pavadinimas] as pav from Archyvas$ where [Lietuviškas pavadinimas] Like '%a%'");
          //  comboBox1.ItemsSource = tb.DefaultView;
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
            progressLabel.Text = e.ProgressPercentage.ToString() + " %";
        }

        private void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                ConfigDataBase cdb = new ConfigDataBase();
                DataTable ctb = cdb.getTable("select * from config where id=1");
                mediaserver = ctb.Rows[0]["mediaserver"].ToString();
                user = ctb.Rows[0]["user"].ToString();
                password = ctb.Rows[0]["password"].ToString();
                path = ctb.Rows[0]["path"].ToString();
              //  folder = comboBox1.Text;// ctb.Rows[0]["folder"].ToString();

                this.comboBox1.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { folder = comboBox2.Text; }));
              //  FtpClient FC = new FtpClient(mediaserver, user, password);
              //  FC.ChangeDir(folder);
                if (worker.CancellationPending == true)
                {
                    e.Cancel = true;
                    return;
                }
               // FC.worker = worker;
                string filename = "";
                this.textBox2.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() => { filename = textBox2.Text; }));
             //   FC.Upload(filename);
                //uploadFIle("ftp://" + mediaserver + "/%2F/"+folder, user, password, filename);
                //string res=UploadFile("ftp://" + mediaserver + "/", filename, user, password, folder);
                UpFile( mediaserver, filename, user, password, folder);
            }
            catch (Exception ex)
            {
                if (!ex.Message.Contains("underlying connection was closed"))
                {
                    MessageBox.Show("Error: " + ex.Message);
                }
            }
        }

        public static void UpFile(string FtpUrl, string fileName, string userName, string password, string UploadDirectory = "")
        {
           /* System.Net.FtpClient.FtpClient ftpc = new System.Net.FtpClient.FtpClient();
            ftpc.Host = FtpUrl;
            ftpc.Credentials = new NetworkCredential(userName, password);
            ftpc.Connect();
            ftpc.SetWorkingDirectory(UploadDirectory);
            using (Stream ostream = ftpc.OpenWrite(fileName))
            {
                try
                {
                    // istream.Position is incremented accordingly to the writes you perform
                }
                finally
                {
                    ostream.Close();
                }
            }*/
            FTPClient ftp = new FTPClient(FtpUrl);
            ftp.Login(userName, password);
            ftp.ConnectMode = FTPConnectMode.PASV;
            ftp.TransferType = FTPTransferType.BINARY;
            ftp.ChDir(UploadDirectory);
            ftp.Put(fileName, System.IO.Path.GetFileName(fileName));
          //  ftp.C
        }


        public static string UploadFile(string FtpUrl, string fileName, string userName, string password, string UploadDirectory = "")
        {
            string PureFileName = new FileInfo(fileName).Name;
            String uploadUrl = String.Format("{0}{1}/{2}", FtpUrl, UploadDirectory, PureFileName);
            FtpWebRequest req = (FtpWebRequest)FtpWebRequest.Create(uploadUrl);
            req.Proxy = null;
            req.Method = WebRequestMethods.Ftp.UploadFile;
            req.Credentials = new NetworkCredential(userName, password);
            req.UseBinary = true;
            req.UsePassive = true;
            byte[] data = File.ReadAllBytes(fileName);
            req.ContentLength = data.Length;
            Stream stream = req.GetRequestStream();
            stream.Write(data, 0, data.Length);
            stream.Close();
            FtpWebResponse res = (FtpWebResponse)req.GetResponse();
            return res.StatusDescription;
        }


        public void uploadFIle(string mediaserver, string user, string password,string filename)
        {
            string fname = System.IO.Path.GetFileName(filename);
            mediaserver = mediaserver + "/" + fname;
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create(mediaserver);
            request.Method = WebRequestMethods.Ftp.UploadFile;
            request.Proxy = null; 
            request.KeepAlive = false;
            request.UseBinary = true;
            request.UsePassive = true;

            request.Timeout = 10000000;
            request.ReadWriteTimeout = 10000000;

            request.Credentials = new NetworkCredential (user,password);

            string folder = System.IO.Path.GetDirectoryName(filename);
            //Directory.SetCurrentDirectory(folder);

            StreamReader sourceStream = new StreamReader(filename);
            byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
            sourceStream.Close();
            request.ContentLength = fileContents.Length;

            Stream requestStream = request.GetRequestStream();
            requestStream.Write(fileContents, 0, fileContents.Length);
            requestStream.Close();

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            response.Close();
        }

        private void worker_RunWorkerCompleted(object sender,
                                               RunWorkerCompletedEventArgs e)
        {
            //update ui once worker complete his work
            buttonImport.IsEnabled = true;
            buttonCancel.IsEnabled = true;
            textBox2.IsEnabled = true;
            button2.IsEnabled = true;
            button3.IsEnabled = true;
          //  progress.Visibility = Visibility.Hidden;
            progressBar1.Visibility = Visibility.Hidden;
            progressLabel.Visibility = Visibility.Hidden;
        }

        private bool checkDurations()
        {
            bool res = false;
            if (!trukme_1.Text.Equals("00:00:00")) { res = true; }
            if (!trukme_2.Text.Equals("00:00:00")) { res = true; }
            if (!tc_trukme.Text.Equals("00:00:00")) { res = true; }
            if (!pradzios_tc.Text.Equals("00:00:00")) { res = true; }
            if (!isikirtimas_1.Text.Equals("00:00:00")) { res = true; }
            if (!isikirtimas_2.Text.Equals("00:00:00")) { res = true; }
            if (!isikirtimas_3.Text.Equals("00:00:00")) { res = true; }
            if (!isikirtimas_4.Text.Equals("00:00:00")) { res = true; }
            if (!isikirtimas_5.Text.Equals("00:00:00")) { res = true; }
            if (!isikirtimas_6.Text.Equals("00:00:00")) { res = true; }
            if (!isikirtimas_7.Text.Equals("00:00:00")) { res = true; }
            if (!isikirtimas_8.Text.Equals("00:00:00")) { res = true; }
            if (!isikirtimas_9.Text.Equals("00:00:00")) { res = true; }
            if (!isikirtimas_10.Text.Equals("00:00:00")) { res = true; }
            if (!isikirtimas_11.Text.Equals("00:00:00")) { res = true; }
            if (!isikirtimas_12.Text.Equals("00:00:00")) { res = true; }
            if (!isikirtimas_13.Text.Equals("00:00:00")) { res = true; }
            if (!isikirtimas_14.Text.Equals("00:00:00")) { res = true; }
            if (!isikirtimas_15.Text.Equals("00:00:00")) { res = true; }
            return res;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string id = comboBox1.SelectedValue.ToString();
            string type = comboBox2.SelectedValue.ToString();
            if ((!textBox2.Text.Equals("")) && (checkDurations()) && comboBox2.Text.Contains("Filmai"))
            {
                string server = "192.168.100.100\\SQLEXPRESS";
                string dbuser = "sa";
                string pass = "tornado";
                string database = "PASystem";
                DataBase db = new DataBase(server, dbuser, pass, database);
                string filename=System.IO.Path.GetFileName(textBox2.Text);    
                DataTable tb=db.getTable("select * from VideoArch where filename='" + filename + "'");
                if (tb.Rows.Count > 0)
                {
                    db.sqlExecute("update VideoArch set filename='" + filename + "', duration='', inserttime=CURRENT_TIMESTAMP, archid="+id+", type='"+type+"' where ID=" + tb.Rows[0]["ID"].ToString());
                }
                else
                {
                   db.sqlExecute("insert into VideoArch (filename,duration,description,inserttime,archid,type) values ('" + filename + "','','',CURRENT_TIMESTAMP,"+id+",'"+type+"')");
                }

                if (!trukme_1.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [Trukmė 1]='" + trukme_1.Text + "' where id=" + id); }
                if (!trukme_2.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [Trukmė 2]='" + trukme_2.Text + "' where id=" + id); }
                if (!tc_trukme.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [TC trukmė]='" + tc_trukme.Text + "' where id=" + id); }
                if (!pradzios_tc.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [Pradzios TC]='" + pradzios_tc.Text + "' where id=" + id); }
                if (!isikirtimas_1.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [I Įsikirtimas]='" + isikirtimas_1.Text + "' where id=" + id); }
                if (!isikirtimas_2.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [II Įsikirtimas]='" + isikirtimas_2.Text + "' where id=" + id); }
                if (!isikirtimas_3.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [III Įsikirtimas]='" + isikirtimas_3.Text + "' where id=" + id); }
                if (!isikirtimas_4.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [IV Įsikirtimas]='" + isikirtimas_4.Text + "' where id=" + id); }
                if (!isikirtimas_5.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [V Įsikirtimas]='" + isikirtimas_5.Text + "' where id=" + id); }
                if (!isikirtimas_6.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [VI Įsikirtimas]='" + isikirtimas_6.Text + "' where id=" + id); }
                if (!isikirtimas_7.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [VII Įsikirtimas]='" + isikirtimas_7.Text + "' where id=" + id); }
                if (!isikirtimas_8.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [VIII Įsikirtimas]='" + isikirtimas_8.Text + "' where id=" + id); }
                if (!isikirtimas_9.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [IX Įsikirtimas]='" + isikirtimas_9.Text + "' where id=" + id); }
                if (!isikirtimas_10.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [X Įsikirtimas]='" + isikirtimas_10.Text + "' where id=" + id); }
                if (!isikirtimas_11.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [XI Įsikirtimas]='" + isikirtimas_11.Text + "' where id=" + id); }
                if (!isikirtimas_12.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [XII Įsikirtimas]='" + isikirtimas_12.Text + "' where id=" + id); }
                if (!isikirtimas_13.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [XIII Įsikirtimas]='" + isikirtimas_13.Text + "' where id=" + id); }
                if (!isikirtimas_14.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [XIV Įsikirtimas]='" + isikirtimas_14.Text + "' where id=" + id); }
                if (!isikirtimas_15.Text.Equals("00:00:00")) { db.sqlExecute("update Archyvas$ set [XV Įsikirtimas]='" + isikirtimas_15.Text + "' where id=" + id); }

                buttonImport.IsEnabled = false;
                textBox2.IsEnabled = false;
                button2.IsEnabled=false;
                button3.IsEnabled=false;
                progress.Visibility = Visibility.Visible;
                progressBar1.Visibility = Visibility.Visible;
                progressLabel.Visibility = Visibility.Visible;
                progressLabel.Text = "0%";
                progressBar1.Value = 0;
                worker.RunWorkerAsync();
            }
            else
            {
                ConfigDataBase cdb = new ConfigDataBase();
                DataTable ctb = cdb.getTable("select * from config where id=1");
                folder = ctb.Rows[0]["folder"].ToString();
                if (!comboBox2.Text.Contains("Filmai"))//(folder.Equals("reklama") || folder.Equals("anonsai"))
                {
                    string server = "192.168.100.100\\SQLEXPRESS";
                    string dbuser = "sa";
                    string pass = "tornado";
                    string database = "PASystem";
                    DataBase db = new DataBase(server, dbuser, pass, database);
                    string filename = System.IO.Path.GetFileName(textBox2.Text);
                    DataTable tb = db.getTable("select * from VideoArch where filename='" + filename + "'");
                    if (tb.Rows.Count > 0)
                    {
                        db.sqlExecute("update VideoArch set filename='" + filename + "', duration='', inserttime=CURRENT_TIMESTAMP, type='" + type + "' where ID=" + tb.Rows[0]["ID"].ToString());
                    }
                    else
                    {
                        db.sqlExecute("insert into VideoArch (filename,duration,description,inserttime,type) values ('" + filename + "','','',CURRENT_TIMESTAMP,'" + type + "')");
                    }

                    buttonImport.IsEnabled = false;
                    textBox2.IsEnabled = false;
                    button2.IsEnabled = false;
                    button3.IsEnabled = false;
                    progressBar1.Visibility = Visibility.Visible;
                    progressLabel.Visibility = Visibility.Visible;
                    worker.RunWorkerAsync();
                }
                else
                {
                    MessageBox.Show("Užpildykite visus laukus!");
                }
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            buttonImport.IsEnabled = true;
            buttonCancel.IsEnabled = true;
            button2.IsEnabled = true;
            button3.IsEnabled = true;
            textBox2.IsEnabled = true;
            progress.Visibility = Visibility.Hidden;
            worker.CancelAsync();
           // worker.Dispose();
        }

        private void button1_Click_1(object sender, RoutedEventArgs e)
        {
            worker.CancelAsync();
            if (cf != null) { cf.Close(); }
            Close();
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            button3.Visibility = Visibility.Hidden;
            cf = new Config_frm();
            ConfigDataBase cdb = new ConfigDataBase();
            DataTable ctb = cdb.getTable("select * from config where id=1");
            mediaserver = ctb.Rows[0]["mediaserver"].ToString();
            user = ctb.Rows[0]["user"].ToString();
            password = ctb.Rows[0]["password"].ToString();
            path = ctb.Rows[0]["path"].ToString();
            folder = ctb.Rows[0]["folder"].ToString();
            cf.textBox1.Text = mediaserver;
            cf.textBox2.Text = user;
            cf.textBox3.Password = password;
            cf.textBox4.Text = path;
            cf.textBox5.Text = folder;

            cf.Height = this.Height;
            cf.Left = this.Left;
            cf.Top = this.Top;
           // cf.Owner = this;
            cf.Show();
            this.Owner = cf;
            this.Activate();
            // this.Focus();
            // cf.Owner = this;
            double leftpos = cf.Left;
            while (leftpos > this.Left - cf.Width + 50)
            {
                leftpos = leftpos - 10;
                cf.Left = leftpos;
                Thread.Sleep(5);
            }
            
        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            move = true;
            this.pt = System.Windows.Forms.Control.MousePosition;
        }

        private void Window_MouseUp(object sender, MouseButtonEventArgs e)
        {
            move = false;
        }

        private void Window_MouseLeave(object sender, MouseEventArgs e)
        {
            move = false;
        }

        private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            if (move)
            {
                this.Left += (System.Windows.Forms.Control.MousePosition.X - this.pt.X);
                this.Top += (System.Windows.Forms.Control.MousePosition.Y - this.pt.Y);
                if (cf != null)
                {
                    cf.Left += (System.Windows.Forms.Control.MousePosition.X - this.pt.X);
                    cf.Top += (System.Windows.Forms.Control.MousePosition.Y - this.pt.Y);
                }
                this.pt = System.Windows.Forms.Control.MousePosition;
            }
        }

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void button2_Click_1(object sender, RoutedEventArgs e)
        {
            ConfigDataBase cdb = new ConfigDataBase();
            DataTable ctb = cdb.getTable("select * from config where id=1");
            path = ctb.Rows[0]["path"].ToString();
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.InitialDirectory = path;
            dlg.FileName = "video"; // Default file name
            dlg.DefaultExt = ".mpg"; // Default file extension
            dlg.Filter = "Video file (.mpg, *.wmv)|*.mpg;*.wmv"; // Filter files by extension 

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results 
            if (result == true)
            {
                // Open document 
                string filename = dlg.FileName;
                textBox2.Text=filename;
            }
        }

        private void comboBox1_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Enter)
                {
                    DataTable tb = dbe.getTable("select top 100 id,[Lietuviškas pavadinimas] as pav from Archyvas$ where [Lietuviškas pavadinimas] Like '%" + comboBox1.Text + "%'");
                    comboBox1.ItemsSource = tb.DefaultView;
                    comboBox1.IsDropDownOpen = true;
                }
            }
            catch (Exception ex) { }
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            TreeViewItem ti = new TreeViewItem();
            ti.ItemTemplate = (DataTemplate)FindResource("TreeEditTemplate");
            treeView1.Items.Add(ti);
        }

        private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                string id = comboBox1.SelectedValue.ToString();
                string server = "192.168.100.100\\SQLEXPRESS";
                string dbuser = "sa";
                string pass = "tornado";
                string database = "PASystem";
                DataBase db = new DataBase(server, dbuser, pass, database);
                string filename = System.IO.Path.GetFileName(textBox2.Text);
                DataTable tb = db.getTable("select * from Archyvas$ where id=" + id);
                if (tb.Rows.Count > 0)
                {
                    if (!tb.Rows[0]["Trukmė 1"].ToString().Equals("")) { trukme_1.Text = tb.Rows[0]["Trukmė 1"].ToString(); }
                    if (!tb.Rows[0]["Trukmė 2"].ToString().Equals("")) { trukme_2.Text = tb.Rows[0]["Trukmė 2"].ToString(); }
                    if (!tb.Rows[0]["TC trukmė"].ToString().Equals("")) { tc_trukme.Text = tb.Rows[0]["TC trukmė"].ToString(); }
                    if (!tb.Rows[0]["TC trukmė"].ToString().Equals("")) { pradzios_tc.Text = tb.Rows[0]["Pradzios TC"].ToString(); }
                    if (!tb.Rows[0]["I Įsikirtimas"].ToString().Equals("")) { isikirtimas_1.Text = tb.Rows[0]["I Įsikirtimas"].ToString(); }
                    if (!tb.Rows[0]["II Įsikirtimas"].ToString().Equals("")) { isikirtimas_2.Text = tb.Rows[0]["II Įsikirtimas"].ToString(); }
                    if (!tb.Rows[0]["III Įsikirtimas"].ToString().Equals("")) { isikirtimas_3.Text = tb.Rows[0]["III Įsikirtimas"].ToString(); }
                    if (!tb.Rows[0]["IV Įsikirtimas"].ToString().Equals("")) { isikirtimas_4.Text = tb.Rows[0]["IV Įsikirtimas"].ToString(); }
                    if (!tb.Rows[0]["V Įsikirtimas"].ToString().Equals("")) { isikirtimas_5.Text = tb.Rows[0]["V Įsikirtimas"].ToString(); }
                    if (!tb.Rows[0]["VI Įsikirtimas"].ToString().Equals("")) { isikirtimas_6.Text = tb.Rows[0]["VI Įsikirtimas"].ToString(); }
                    if (!tb.Rows[0]["VII Įsikirtimas"].ToString().Equals("")) { isikirtimas_7.Text = tb.Rows[0]["VII Įsikirtimas"].ToString(); }
                    if (!tb.Rows[0]["VIII Įsikirtimas"].ToString().Equals("")) { isikirtimas_8.Text = tb.Rows[0]["VIII Įsikirtimas"].ToString(); }
                    if (!tb.Rows[0]["IX Įsikirtimas"].ToString().Equals("")) { isikirtimas_9.Text = tb.Rows[0]["IX Įsikirtimas"].ToString(); }
                    if (!tb.Rows[0]["X Įsikirtimas"].ToString().Equals("")) { isikirtimas_10.Text = tb.Rows[0]["X Įsikirtimas"].ToString(); }
                    if (!tb.Rows[0]["XI Įsikirtimas"].ToString().Equals("")) { isikirtimas_11.Text = tb.Rows[0]["XI Įsikirtimas"].ToString(); }
                    if (!tb.Rows[0]["XII Įsikirtimas"].ToString().Equals("")) { isikirtimas_12.Text = tb.Rows[0]["XII Įsikirtimas"].ToString(); }
                    if (!tb.Rows[0]["XIII Įsikirtimas"].ToString().Equals("")) { isikirtimas_13.Text = tb.Rows[0]["XIII Įsikirtimas"].ToString(); }
                    if (!tb.Rows[0]["XIV Įsikirtimas"].ToString().Equals("")) { isikirtimas_14.Text = tb.Rows[0]["XIV Įsikirtimas"].ToString(); }
                    if (!tb.Rows[0]["XV Įsikirtimas"].ToString().Equals("")) { isikirtimas_15.Text = tb.Rows[0]["XV Įsikirtimas"].ToString(); }
                    if (!tb.Rows[0]["I Įsikirtimas"].ToString().Equals("")) { isikirtimas_1.Text = tb.Rows[0]["I Įsikirtimas"].ToString(); }
                    if (!tb.Rows[0]["Filmo_ID"].ToString().Equals("")) { textBox1.Text = tb.Rows[0]["Filmo_ID"].ToString(); }
                }
            }
            catch (Exception ex) { }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }

        private void comboBox2_Loaded(object sender, RoutedEventArgs e)
        {
            comboBox2.SelectedIndex =1 ;
            List<string> data = new List<string>();
             data.Add("Filmai");
            data.Add("Vinjetes");
            data.Add("Laidos");
            data.Add("Reklama");
            data.Add("Anonsai");
            data.Add("Serialai");

            var comboBox = sender as ComboBox;
            comboBox.ItemsSource = data;
            comboBox.SelectedIndex = 0;
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
            this.Owner = null;
            foreach (Window wd in Application.Current.Windows)
            {
                if (wd.Title.Equals("Preview_frm"))
                {
                    wd.Visibility = Visibility.Hidden;
                    //  wd.WindowState = WindowState.Minimized;
                }
                if (wd.Title.Equals("TC_frm"))
                {
                    wd.Visibility = Visibility.Hidden;
                    //  wd.WindowState = WindowState.Minimized;
                }
                if (wd.Title.Equals("Config_frm"))
                {
                    wd.Visibility = Visibility.Hidden;
                    // wd.WindowState = WindowState.Minimized;
                }
            }
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Normal)
            {
                foreach (Window wd in Application.Current.Windows)
                {
                    if (wd.Title.Equals("Config_frm"))
                    {
                        wd.Visibility = Visibility.Visible;
                        // wd.WindowState = WindowState.Normal;
                    }
                }
                orderWindows(null);
            }
        }

        public Window getWindows(string name)
        {
            Window wt = null;
            foreach (Window wd in Application.Current.Windows)
            {
                if (wd.Title.Equals(name))
                {
                    wt = wd;
                }
            }
            return wt;
        }

        public void orderWindows(Window main)
        {
            bool widen = false;
            Window mn = getWindows("VideoArchive");
            if (mn != null)
            {
                mn.Owner = null;
            }
            Window cf = getWindows("Config_frm");
            if (cf != null)
            {
                cf.Owner = null;
            }
            if (cf != null)
            {
                mn.Owner = cf;
                mn.Activate();
            }
        }

        private void Image_ImageFailed_1(object sender, ExceptionRoutedEventArgs e)
        {

        }
    }
}
